/*
 *可用优惠券列表
 */
require(['jquery','../modules/pageSize','../modules/popup', 'jqScroller'],function($,pageSetSize,popup){
	//设置html根目录字体大小
    setRootFontSize(pageSetSize);
    var curPagePos = 1;
	$(".progress-loader a").on("click", function(e){
        e.preventDefault();
		if(jqScroller.isBlock()) return;
		jqScroller.block();
        jqScroller.render(curPagePos);
    });
	/*请求优惠券列表*/
	jqScroller.render = function(cpage){
        popup.loadPopup("正在加载中...");
        $.ajax({
            type: "GET",
            url: "/Home/Member/apimemberCoupon"+getFixedExtraUrl(),
            data: { "page" : cpage},
            dataType: "json",
            success: function (rs) {
                popup.removeLoad();
				if(rs && rs.data != null)  {
                    renderCouponList(rs.data);
					curPagePos++;
                    jqScroller.curPage++;
                    jqScroller.unblock();
                    $(".progress-loader").css("display", "");
                }else{
					jqScroller.done();
                    $(".progress-loader").css("display", "none");
                    if(curPagePos == 1){
                        $("#yc_sx_empty").css("display", "block");    
                    }else{                        
                        popup.alertPopup("没有更多数据！");
                    }                    
                }              
            },
            error: function () {
                popup.removeLoad();
                popup.alertPopup('服务器异常，请稍后重试！');
                jqScroller.unblock();
            }
        });
    };
    jqScroller.run();
    jqScroller.render(1);
	/*渲染优惠券列表*/
	function renderCouponList(list){
		var output = "";
		var c;
		for(var k in list){
			c = list[k];
			if(c["willFail"] == 1){
				output += '<div class="coupon-item willFail" data-id="'+c["cid"]+'">';
			}else{
				output += '<div class="coupon-item" data-id="'+c["cid"]+'">';
			}
			output += '<h1>'+c["name"]+'</h1>';
			output += '<h5>'+c["description"]+'</h5>';
			output += '<h5 style="width:100%">有效期:'+c["start_time"]+' 至 '+c["end_time"]+'</h5>';
			output += '<span class="coupon-shop" onclick="jump(\'/store/index/local_id/'+c["local_id"]+''+getFixedExtraUrl("local_id")+'\')" style="z-index:9"><i><img src="/Public/Home/images/shop-icon-coupan.png"> </i>'+c["local_name"]+'</span>';
			if(c["type"] == 3){
				output += '<a href="/Home/Member/memberCouponGoodsList/rule_id/'+c["id"]+''+getFixedExtraUrl()+'">可用商品 &gt; </a>';
			}
			if((c['type'] == 2 || c['type'] == 1) && c['goods_id_array'] != ''){
                output += '<a href="/Home/Member/memberGoodsNoUse/rule_id/'+c["id"]+''+getFixedExtraUrl()+'" class="coupon-tips"><img src="/Public/Home/images/coupon-tips-icon.png"></a>';
            }

			output += '</div>';
		}
		if(curPagePos % 10 == 0){
			$("#ajax-couponlist").html(output);	
		}else{
			$("#ajax-couponlist").append(output);
		}
	}
	// 优惠券选择效果
    $('.coupon-get a').on('click',function(){
        var currInputValue = $('.coupon-get input').val();
        if(currInputValue ==''){
            popup.alertPopup('请输入兑换码!');
        }else{
            $.ajax({
                url:'/Home/Member/ecCode'+getFixedExtraUrl(),
                type:'post',
                dataType:'json',
                data:{'code':$('#dhcode').val()},
                success:function(rs){
					popup.alertPopup(rs.msg);
					if(rs.status == 1){
					   setTimeout(function(){
						  window.location.reload();
					   },1000);
					}
                },
                error:function(){
                    popup.alertPopup('服务器异常，请稍后重试！');
                }
            })
        }
    }); 
});